<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
function fontSize(font) {
  return (window.innerWidth / 1920) * font;
}
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    pieData: {
      type: Object,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    pieData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(this.pieData);
    },
    setOptions(pieData) {
      let tooltip = {
        ...{
          trigger: "item",
          formatter: "{b} : {c} ({d}%)",
          backgroundColor: "rgba(0,0,0,0.7)",
          borderColor: "transparent",
          textStyle: {
            color: "#fff",
            fontSize: 14,
          },
        },
        ...pieData.tooltip,
      };
      this.chart.setOption({
        title: pieData.title ? pieData.title : {},
        tooltip: tooltip,
        color: pieData.color ? pieData.color : [],
        legend: pieData.legend,

        series: pieData.series,
      });
    },
  },
};
</script>
